import java.util.Scanner;

public class ConversationDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        LinearList list1 = new LinearList();
        LinearList list2 = new LinearList("hello");


        System.out.println("欢迎来到对话程序！");

        boolean running = true;
        while (running) {
            System.out.println("请输入指令" +
                    "添加元素指令：add" +
                    "移除元素指令：remove" +
                    "add/remove/contains/union/intersection/difference/exit）：");
            String command = scanner.nextLine().trim();

            switch (command) {
                case "add":
                    System.out.println("请输入要添加的小写字母元素：");
                    char addElement = scanner.nextLine().trim().charAt(0);
                    try {
                        list1.add(addElement);
                        System.out.println("元素 " + addElement + " 添加成功！");
                    } catch (IllegalArgumentException e) {
                        System.out.println("输入的不是小写字母，请重新输入！");
                    }
                    break;

                case "remove":
                    System.out.println("请输入要移除的小写字母元素：");
                    char removeElement = scanner.nextLine().trim().charAt(0);
                    try {
                        if (list1.remove(removeElement)) {
                            System.out.println("元素 " + removeElement + " 移除成功！");
                        } else {
                            System.out.println("元素 " + removeElement + " 不存在！");
                        }
                    } catch (IllegalArgumentException e) {
                        System.out.println("输入的不是小写字母，请重新输入！");
                    }
                    break;

                case "contains":
                    System.out.println("请输入要查询的小写字母元素：");
                    char containsElement = scanner.nextLine().trim().charAt(0);
                    try {
                        if (list1.contains(containsElement)) {
                            System.out.println("元素 " + containsElement + " 存在于线性表中！");
                        } else {
                            System.out.println("元素 " + containsElement + " 不存在于线性表中！");
                        }
                    } catch (IllegalArgumentException e) {
                        System.out.println("输入的不是小写字母，请重新输入！");
                    }
                    break;

                case "union":
                    LinearList unionList = list1.union(list2);
                    System.out.println("集合的并集：" + new String(unionList.getElements()));
                    break;

                case "intersection":
                    LinearList intersectionList = list1.intersection(list2);
                    System.out.println("集合的交集：" + new String(intersectionList.getElements()));
                    break;

                case "difference":
                    LinearList differenceList = list1.difference(list2);
                    System.out.println("集合的差集：" + new String(differenceList.getElements()));
                    break;

                case "exit":
                    running = false;
                    break;

                default:
                    System.out.println("无效的指令，请重新输入！");
                    break;
            }

            // 打印集合内容
            System.out.println("集合1：" + new String(list1.getElements()));
            System.out.println("集合2：" + new String(list2.getElements()));

            System.out.println();
        }

        System.out.println("对话程序已退出。");
    }
}